/*
 * Copyright (c) 2021 hpmicro
 *
 * SPDX-License-Identifier: BSD-3-Clause
 *
 */

#include <stdio.h>
#include "board.h"
#include "hpm_gpio_drv.h"

#define LED_FLASH_PERIOD_IN_MS 500

static void init_butn_as_gpio()
{
    uint32_t pad_ctl = IOC_PAD_PAD_CTL_PE_SET(1) | IOC_PAD_PAD_CTL_PS_SET(1);
    // 设置PBUTN（PZ02）、WBUTN（PZ03）为GPIO功能
    HPM_BIOC->PAD[IOC_PAD_PZ02].FUNC_CTL = IOC_PZ02_FUNC_CTL_BGPIO_Z_02;
    HPM_BIOC->PAD[IOC_PAD_PZ03].FUNC_CTL = IOC_PZ03_FUNC_CTL_BGPIO_Z_03;

    // 设置PZ02、PZ03为GPIO的模式为 内部上拉
    HPM_IOC->PAD[IOC_PAD_PZ02].PAD_CTL = pad_ctl;
    HPM_IOC->PAD[IOC_PAD_PZ03].PAD_CTL = pad_ctl;
}

void app_led_write(uint32_t index, uint8_t state)
{
    switch (index)
    {
    case 0:
        gpio_write_pin(BOARD_R_GPIO_CTRL, BOARD_R_GPIO_INDEX, BOARD_R_GPIO_PIN, state);
        break;
    case 1:
        gpio_write_pin(BOARD_G_GPIO_CTRL, BOARD_G_GPIO_INDEX, BOARD_G_GPIO_PIN, state);
        break;
    case 2:
        gpio_write_pin(BOARD_B_GPIO_CTRL, BOARD_B_GPIO_INDEX, BOARD_B_GPIO_PIN, state);
        break;
    default:
        /* Suppress the toolchain warnings */
        break;
    }
}

uint32_t app_led_next(uint32_t index)
{
    return (index + 1) % 3;
}

uint32_t app_led_prev(uint32_t index)
{
    return (index + 3 - 1) % 3;
}


int main(void)
{
    board_init();
    board_init_led_pins();

    printf("gpio_led start...\n");
#if 0
    board_timer_create(LED_FLASH_PERIOD_IN_MS, board_led_toggle);
    while (1) board_delay_ms(1000);
#endif

#if 0
    while(1)
    {
        gpio_write_pin(BOARD_R_GPIO_CTRL, BOARD_R_GPIO_INDEX, BOARD_R_GPIO_PIN, 1);
        board_delay_ms(LED_FLASH_PERIOD_IN_MS);
        gpio_write_pin(BOARD_R_GPIO_CTRL, BOARD_R_GPIO_INDEX, BOARD_R_GPIO_PIN, 0);
        board_delay_ms(LED_FLASH_PERIOD_IN_MS);

        gpio_write_pin(BOARD_G_GPIO_CTRL, BOARD_G_GPIO_INDEX, BOARD_G_GPIO_PIN, 1);
        board_delay_ms(LED_FLASH_PERIOD_IN_MS);
        gpio_write_pin(BOARD_G_GPIO_CTRL, BOARD_G_GPIO_INDEX, BOARD_G_GPIO_PIN, 0);
        board_delay_ms(LED_FLASH_PERIOD_IN_MS);

        gpio_write_pin(BOARD_B_GPIO_CTRL, BOARD_B_GPIO_INDEX, BOARD_B_GPIO_PIN, 1);
        board_delay_ms(LED_FLASH_PERIOD_IN_MS);
        gpio_write_pin(BOARD_B_GPIO_CTRL, BOARD_B_GPIO_INDEX, BOARD_B_GPIO_PIN, 0);
        board_delay_ms(LED_FLASH_PERIOD_IN_MS);
    }
#endif

    uint32_t current = 0, next = 0;
    init_butn_as_gpio();
    while (1)
    {
        // 读取按键状态
        uint8_t pbutn_value = gpio_read_pin(HPM_BGPIO, GPIO_DI_GPIOZ, 2);
        uint8_t wbutn_value = gpio_read_pin(HPM_BGPIO, GPIO_DI_GPIOZ, 3);

        // 按键状态处理
        if (pbutn_value == 0) {
            printf("pbutn_value=%d, wbutn_value=%d\n", pbutn_value, wbutn_value);
            next = app_led_next(current);
            app_led_write(current, 0);
            //app_led_write(next, 1); 
            current = next;
        } else if (wbutn_value == 0) {
            printf("pbutn_value=%d, wbutn_value=%d\n", pbutn_value, wbutn_value);
            next = app_led_prev(current);
            app_led_write(current, 0);
            //app_led_write(next, 1); 
            current = next;
        }
        app_led_write(current, 1); // 第一次能够点亮红色

        // 延时，控制扫描频率
        board_delay_ms(100);
    }
    return 0;
}
